import re

from func.dao import Database

def is_valid_email(email: str):
    """检查邮箱格式"""
    pattern = r'^[a-zA-Z0-9._%+-]+@(stu\.)?pku\.edu\.cn$'
    return bool(re.match(pattern, email))

def register_user(db: Database, email: str, password: str, nickname: str = ''):
    """注册新用户"""
    # 检查邮箱是否已存在
    user = db.get_user_by_email(email)
    if user:
        raise Exception('该邮箱已被注册')
    
    # 添加新用户
    user_id = db.add_user({
        'nickname': nickname,
        'description': "该用户正在潜水",
        'avatar': 'images/default_head.jpg',
        'email': email,
        'password': password,
        'address': '暂无常用地址'
    })
    return user_id

def login_user(db: Database, email: str, password: str):
    """用户登录"""
    user = db.get_user_by_email(email)
    
    if not user or user['password'] != password:
        raise Exception('邮箱或密码错误')
    
    return user

def get_user_profile(db: Database, email: str):
    """获取用户资料"""
    # 获取用户基本信息
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    return user

def get_user_profile_by_id(db: Database, user_id: int):
    """获取用户资料"""
    # 获取用户基本信息
    user = db.get_user_by_id(user_id)
    if not user:
        raise Exception('用户不存在')
    
    return user

def get_user_addresses(db: Database, email: str):
    """获取用户地址列表"""
    # 首先通过邮箱获取用户ID
    user = db.get_user_by_email(email)
    if not user:
        return []
    
    # 获取用户的地址
    return db.get_user_addresses(user['id'])

def get_address_by_id(db: Database, address_id: int):
    """获取用户地址"""
    return db.get_address(address_id)

def add_user_address(db: Database, email: str, address: str):
    """添加用户地址"""
    # 首先通过邮箱获取用户ID
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 添加地址
    address_id = db.add_address({
        'user_id': user['id'],
        'address': address
    })
    return address_id

def update_user_address(db: Database, email: str, new_address: str):
    """更新用户地址"""
    # 获取用户信息
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 更新用户地址
    return db.update_user(user['id'], {
        'nickname': user['nickname'],
        'description': user['description'],
        'avatar': user['avatar'],
        'email': user['email'],
        'password': user['password'],
        'address': new_address
    })

def delete_user_address(db: Database, email: str, address_id: int):
    """删除用户地址"""
    # 首先通过邮箱获取用户ID
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 删除地址
    return db.delete_address(address_id)

def update_user_avatar(db: Database, email: str, new_avatar_path: str):
    """更新用户头像路径"""
    # 获取用户信息
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 更新用户头像路径
    return db.update_user(user['id'], {
        'nickname': user['nickname'],
        'description': user['description'],
        'avatar': new_avatar_path,
        'email': user['email'],
        'password': user['password'],
        'address': user['address']
    })

def update_user_password(db: Database, email: str, new_password: str):
    """更新用户密码"""
    # 获取用户信息
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 更新用户密码
    return db.update_user(user['id'], {
        'nickname': user['nickname'],
        'description': user['description'],
        'avatar': user['avatar'],
        'email': user['email'],
        'password': new_password,
        'address': user['address']
    })

def add_verification_code(db: Database, email: str, code: str, expired_at: int):
    """添加验证码"""
    # 检查邮箱格式
    if not is_valid_email(email):
        raise Exception('邮箱格式不正确')
    
    # 添加验证码
    return db.add_verification_code(email, code, expired_at)

def get_verification_code(db: Database, email: str):
    """获取验证码"""
    # 检查邮箱格式
    if not is_valid_email(email):
        raise Exception('邮箱格式不正确')
    
    # 获取验证码
    return db.get_verification_code(email)

def delete_verification_code(db: Database, email: str):
    """删除验证码"""
    # 检查邮箱格式
    if not is_valid_email(email):
        raise Exception('邮箱格式不正确')
    
    # 删除验证码
    return db.delete_verification_code(email)

def clean_expired_codes(db: Database):
    """清理过期的验证码"""
    return db.clean_expired_codes()

def update_user_description(db: Database, email: str, new_description: str):
    """更新用户描述"""
    # 获取用户信息
    user = db.get_user_by_email(email)
    if not user:
        raise Exception('用户不存在')
    
    # 更新用户描述
    return db.update_user(user['id'], {
        'nickname': user['nickname'],
        'description': new_description,
        'avatar': user['avatar'],
        'email': user['email'],
        'password': user['password']
    }) 